package cate.game.role.res.guild.po.my;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDGuild;
import cate.common.table.d.GDObj;
import cate.common.table.guild.GuildActiveTaskRow;
import cate.common.table.guild.GuildDonateBoxRow;
import cate.game.attr.FightAttr;
import cate.game.event.guild.GuildAchieveTaskEvent;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.res.guild.msg.MyGuildActiveResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "我的公会活跃信息")
public class MyGuildActivePO extends MyGuildFuncBase {

    @NoteField(value = "累计活跃经验")
    public int activeExp;

    @NoteField(value = "今日已获得活跃经验")
    public int activeExpToday;

    @NoteField(value = "本周已获得活跃经验")
    public int activeExpWeek;

    @NoteField(value = "日常任务完成量")
    public int dayTaskCount;

    @NoteField(value = "宝箱领取记录", detail = "<宝箱ID, 是否领取>")
    public Map<Integer, Boolean> boxRecord;

    @NoteField(
            value = "公会活跃详情记录",
            detail = "<活跃任务ID, 目标次数总计>")
    public Map<Integer, Integer> activeTaskMap;

    @NoteField(
            value = "公会活跃详情记录",
            detail = "<活跃任务ID, 完成任务量")
    public Map<Integer, Integer> activeShowMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (activeTaskMap == null) {
            activeTaskMap = new HashMap<>();
        }
        if (activeShowMap == null) {
            activeShowMap = new HashMap<>();
        }
        if (boxRecord == null) {
            boxRecord = new HashMap<>();
        }
    }

    /**
     * 领取公会活跃度宝箱
     */
    public void getActiveBox(int boxId) {
        if(!guildFuncOpenCheck()){
            return;
        }
        Boolean got = boxRecord.get(boxId);
        if (got != null) {
            role.getGame().notice.message(role, 310411, "奖励已领取");
            return;
        }
        GuildDonateBoxRow row = role.getGame().table.guild.donateBox.get(boxId);
        if (this.activeExp < row.expRequird) {
            role.getGame().notice.message(role, 310412, "活跃度不足");
            return;
        }
        MixRes reward = new MixRes(row.rewardCfg);

        reward.addItem(new MixResItem(GDObj.Type.NUM, GDObj.Num.P, row.PNum));
        EcResult<?> r_add = reward.add(role);
        if (!r_add.ok()) {
            role.getGame().notice.message(role, "物品添加失败");
            return;
        }
        role.getRes().guild.performance.addPValue(row.PNum);
        boxRecord.put(boxId, true);
        role.getGame().notice.dialogReward(role, reward);
        // 通知我的公会宝箱变更
        noticeUpdate();
    }


    /**
     * 跨天更新
     */
    @JsonIgnore
    public void onDaySpan() {
        activeExpToday = 0;
        for (GuildActiveTaskRow row : role.getGame().table.guild.activeTask.list) {
            if (row.cycle == GDGuild.refreshType.t0) {
                activeTaskMap.put(row.id, 0);
                activeShowMap.put(row.id, 0);
            }
        }
        this.dayTaskCount = 0;
        noticeUpdate();
    }

    /**
     * 跨周更新
     */
    @JsonIgnore
    public void onWeekSpan() {
        activeExpWeek = 0;
        for (GuildActiveTaskRow tpl : role.getGame().table.guild.activeTask.list) {
            if (tpl.cycle == GDGuild.refreshType.t1) {
                activeTaskMap.put(tpl.id, 0);
                activeShowMap.put(tpl.id, 0);
            }
        }
        noticeUpdate();
    }

    /**
     * 公会活跃任务记录
     */
    @JsonIgnore
    public void addActive(int taskId) {
        if (!guildFuncOpenCheck()) {
            return;
        }
        GuildActiveTaskRow taskRow = role.getGame().table.guild.activeTask.get(taskId);
        if (taskRow == null) {
            return;
        }

        if (taskRow.times <= GDGuild.activeTask.ZERO) {
            logger.info("活跃任务-非法配置");
            return;
        }

        Integer num = activeTaskMap.get(taskId);
        if (num == null) {
            num = 0;
        }
        int show;
        // 任务完成次数未达到上限时
        if (num < taskRow.limitNum) {
            num++;
            activeTaskMap.put(taskId, num);

            show = num / taskRow.times;
            int originFinishTimes = activeShowMap.getOrDefault(taskId, 0);
            activeShowMap.put(taskId, show);
            if (num == (originFinishTimes + 1) * taskRow.times) {
                onFinishTask(taskRow);
            }
        }
        this.noticeUpdate();
    }

    private void onFinishTask(GuildActiveTaskRow taskRow) {
        //统计日常任务完成量
        if (taskRow.cycle == GDGuild.refreshType.t0) {
            dayTaskCount++;
            role.getRes().guild.performance.snycDayTask(dayTaskCount);
        }

//        MixRes reward = new MixRes(taskRow.rewardStr);
//        reward.add(role);
        addExp(taskRow.exp);
        publishEvent(role, new GuildAchieveTaskEvent(role, role.getRes().guild.getGuild(), new MixRes()));
    }

    /**
     * 增加活跃经验
     */
    public void addExp(int exp) {
        //增加我的活跃度
        addMyExp(exp);
        //增加我的数据中的公会表现中的活跃度
        role.getRes().guild.performance.addActive(exp);
        //增加公会活跃度
        role.getRes().guild.getGuild().active.addActive(exp);
    }

    /**
     * 增加活跃经验
     */
    private void addMyExp(int exp) {
        activeExp += exp;
        activeExpToday += exp;
        activeExpWeek += exp;
    }

    public void noticeUpdate() {
        role.sendNow(new MyGuildActiveResp(this));
    }
// 按照玩家已经领取 的宝箱来计算属性
//    public FightAttr onGetAttr() {
//        FightAttr attr = new FightAttr();
//        for (Integer boxId : boxRecord.keySet()) {
//            GuildDonateBoxRow row = role.getGame().table.guild.donateBox.get(boxId);
//            if (row == null) {
//                continue;
//            }
//            attr.add(new FightAttr(row.attrStr));
//        }
//        return attr;
//    }

    /**
     * 按照玩家能够领取的宝箱来计算属性
     */
    public FightAttr onGetAttr() {
        GuildDonateBoxRow cur = null;
        for (GuildDonateBoxRow row : role.getGame().table.guild.donateBox.getList()) {
            if (this.activeExp > row.expRequird) {
                cur = row;
            } else {
                break;
            }
        }
        FightAttr attr = new FightAttr();
        if (cur != null) {
            attr.add(new FightAttr(cur.attrStr));
        }
        return attr;
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.TASK;
    }
}
